package com.codedancer.campus.manager.api.role;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codedancer.campus.common.anno.InterfacePermission;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.response.ApiResponse;
import com.codedancer.campus.manager.api.role.ro.RoleAddOrUpdateRo;
import com.codedancer.campus.manager.api.role.ro.RolePageSearchRo;
import com.codedancer.campus.manager.service.IRoleService;
import com.codedancer.campus.manager.service.bo.role.RoleAddOrUpdateBo;
import com.codedancer.campus.manager.service.bo.role.RolePageSearchBo;
import com.codedancer.campus.manager.service.vo.role.RolePageVo;
import com.codedancer.campus.manager.service.vo.role.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/role")
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "角色Api", description = "角色Api", tags = "角色Api")
public class RoleApi extends BaseApi {

    @Autowired
    private IRoleService roleService;

    @PostMapping("/add")
    @ApiOperation("添加角色")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> add(
        @RequestBody @Valid @ApiParam(value = "角色添加Ro", required = true)
        RoleAddOrUpdateRo roleAddOrUpdateRo) {
        RoleAddOrUpdateBo roleAddOrUpdateBo = BeanUtil.copyProperties(roleAddOrUpdateRo,
            RoleAddOrUpdateBo.class);
        roleService.add(roleAddOrUpdateBo);
        return success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询角色")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<IPage<RolePageVo>> page(@ApiParam(value = "角色分页搜索Ro", required = false)
    RolePageSearchRo rolePageSearchRo) {
        RolePageSearchBo rolePageSearchBo = BeanUtil.copyProperties(rolePageSearchRo,
            RolePageSearchBo.class);
        return success(roleService.page(rolePageSearchBo));
    }

    @PutMapping("/update/{uuid}")
    @ApiOperation("修改角色")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> update(
        @RequestBody @Valid @ApiParam(value = "角色添加Ro", required = true)
        RoleAddOrUpdateRo roleAddOrUpdateRo,
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "角色uuid", required = true) String uuid) {
        RoleAddOrUpdateBo roleAddOrUpdateBo = BeanUtil.copyProperties(roleAddOrUpdateRo,
            RoleAddOrUpdateBo.class);
        roleAddOrUpdateBo.setUuid(uuid);
        roleService.update(roleAddOrUpdateBo);
        return success();
    }

    @GetMapping("/all")
    @ApiOperation("获取全部角色")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<List<RoleVo>> all() {
        return success(roleService.all());
    }

    @PostMapping("/assign-permissions/{roleUuid}")
    @ApiOperation("分配权限")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> assignPermissions(
        @RequestBody @ApiParam(value = "分配的菜单uuids") List<String> menuUuids,
        @PathVariable(value = "roleUuid", required = true) @ApiParam(value = "角色uuid", required = true) String roleUuid) {
        roleService.assignPermissions(menuUuids, roleUuid);
        return success();
    }

    @GetMapping("/list-menu-by-role-uuid/{roleUuid}")
    @ApiOperation("获取角色菜单权限")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<List<String>> listMenuByRoleUuid(
        @PathVariable(value = "roleUuid", required = true) @ApiParam(value = "角色uuid", required = true) String roleUuid) {
        return success(roleService.listMenuByRoleUuid(roleUuid));
    }
}
